//-----------------------------------------------------------------------
// <copyright file="StringReplace.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
// </copyright>
// <author>Shaun Wilde</author>
// <email>v-swilde</email>
// <date>2005-12-06</date>
// <summary>
// Replaces values in a string with new values
// </summary>
//-----------------------------------------------------------------------

namespace Microsoft.Sdc.Tasks
{
    #region Using directives
    using System;
    using System.Collections.Generic;
    using System.Text;
    using Microsoft.Build.BuildEngine;
    using Microsoft.Build.Framework;
    using Microsoft.Build.Utilities;
    using Microsoft.Build.Shared;
    #endregion

    public class StringReplace : TaskBase
    {
        private string inputString = String.Empty;
        private string oldString = String.Empty;
        private string newString = String.Empty;
        private string outputString = String.Empty;

        /// <summary>
        /// The string that is to be manipulated
        /// </summary>
        [Required]
        public string Input
        {
            get
            {
                if (string.IsNullOrEmpty(inputString))
                    return "";
                return inputString;
            }
            set
            {
                inputString = value;
            }
        }

        /// <summary>
        /// The string to replace in the <see cref="Input"/> string.
        /// </summary>
        [Required]
        public string OldValue
        {
            get
            {
                if (string.IsNullOrEmpty(oldString))
                    return "";
                return oldString;
            }
            set
            {
                oldString = value;
            }
        }

        /// <summary>
        /// The string to replace with in the <see cref="Input"/> string.
        /// </summary>
        public string NewValue
        {
            get
            {
                if (string.IsNullOrEmpty(newString))
                    return "";
                return newString;
            }
            set
            {
                newString = value;
            }
        }

        /// <summary>
        /// The resulting output string.
        /// </summary>
        [Output]
        public string Output
        {
            get
            {
                return outputString;
            }
        }

        /// <summary>
        /// required workhorse
        /// </summary>
        protected override void InternalExecute()
        {
            outputString = Input.Replace(OldValue, NewValue);
        }
    }
}
